package com.bank.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateUtil {

    public static Date ConvertStringToDate(String pattern, String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        DateTime dateTime = ConvertStringToDateTime(pattern, dateStr);
        if (dateTime == null) {
            return null;
        }
        return dateTime.toDate();
    }


    public static DateTime ConvertStringToDateTime(String pattern,
                                                   String dateStr) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
        DateTime dateTime = null;
        dateTime = formatter.parseDateTime(dateStr);
        return dateTime;
    }

    public static String ConvertDateToString(String pattern, Date date) {
        DateFormat formatPattern = new SimpleDateFormat(pattern);
        return formatPattern.format(date);
    }
}
